package com.rxyb.upms.service.logic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rxyb.core.constants.DbConstants;
import com.rxyb.upms.db.bean.SysUser;
import com.rxyb.upms.db.dao.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author YJH
 * @version 1.0
 * @description 用户逻辑表
 * @date 2020/5/26  19:53
 */
@Component
public class SysUserLogic {

    /**
     * 用户信息
     */
    private final SysUserMapper sysUserMapper;

    public SysUserLogic(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     * @return
     */
    public SysUser findUserName(String username) {
        SysUser user = new SysUser();
        user.setUsername(username);
        return sysUserMapper.selectOne(condition(user));
    }


    /**
     * 参数拼装
     *
     * @param user
     * @return
     */
    private QueryWrapper<SysUser> condition(SysUser user) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(user.getUsername())) {
            queryWrapper.lambda().eq(SysUser::getUsername, user.getUsername());
        }
        queryWrapper.lambda().eq(SysUser::getRowStatus, DbConstants.ROW_STATE_VALID);
        return queryWrapper;
    }
}
